package com.boruan.qq.zxgylibrary.service.manager;


import android.text.TextUtils;
import android.util.Log;

import com.boruan.qq.zxgylibrary.BuildConfig;
import com.boruan.qq.zxgylibrary.constants.ConstantInfo;
import com.boruan.qq.zxgylibrary.service.model.AddressBackPriceBean;
import com.boruan.qq.zxgylibrary.service.model.AddressListBean;
import com.boruan.qq.zxgylibrary.service.model.AppInfo;
import com.boruan.qq.zxgylibrary.service.model.DownOrderBean;
import com.boruan.qq.zxgylibrary.service.model.FirstBannerBean;
import com.boruan.qq.zxgylibrary.service.model.FirstCategoryBean;
import com.boruan.qq.zxgylibrary.service.model.InsuranceUrl;
import com.boruan.qq.zxgylibrary.service.model.OrderDetailBean;
import com.boruan.qq.zxgylibrary.service.model.PayInfoResponse;
import com.boruan.qq.zxgylibrary.service.model.PayResultBean;
import com.boruan.qq.zxgylibrary.service.model.ResponseModel;
import com.boruan.qq.zxgylibrary.service.model.SettleDefaultAddressBean;
import com.boruan.qq.zxgylibrary.service.model.ShopCarDataBean;
import com.convenient.qd.core.bean.BaseResBean;
import com.convenient.qd.core.net.BaseHttpObserver;
import com.convenient.qd.core.net.RefuseHttpObserver;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by 崔琦 on 2019/10/22.
 * des: 国信接口管理类
 */

public class GXDrugModule {

    private static GXDrugModule model;
    private GXDrugApi mApiService;

    private GXDrugModule() {
        mApiService = com.convenient.qd.core.net.RetrofitHelper.getInstance(ConstantInfo.baseInterfaceUrl).create(GXDrugApi.class);
    }

    public static GXDrugModule getInstance(){
        if(model == null) {
            model = new GXDrugModule();
        }
        return model;
    }

    /**
     * 1、获取首页轮播图
     */
    public void getFirstBanner(BaseHttpObserver<BaseResBean<List<FirstBannerBean.ResultBean>>> observer) {

        mApiService.getFirstBanner(new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 2、获取首页分类
     */
    public void getFirstCategoryData(BaseHttpObserver<BaseResBean<List<FirstCategoryBean.ResultBean>>> observer) {
        mApiService.getFirstCategory(new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3、上传经纬度
     */
    public void saveUpdateLocation(String lat,String lng,BaseHttpObserver<BaseResBean<ResponseModel>> observer) {

        mApiService.saveUpdateLocation(lat,lng,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3、1 上传经纬度
     */
    public void saveUpdateLocationOne(String lat,String lng,RefuseHttpObserver<ResponseBody> observer) {

        mApiService.saveUpdateLocationOne(lat,lng,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 4、添加地址
     */
    public void addAddress(String userName,String userPhone,String address,String detailAddress,String fullAddress,int isDefault,int sex,String cityName,int tag,double lat,double lng,String adCode,BaseHttpObserver<BaseResBean> observer) {
        Map map = new HashMap();
        map.put("contactperson",userName);
        map.put("contactphone",userPhone);
        map.put("address",address);
        map.put("fulladdress",fullAddress);
        map.put("attachaddress",detailAddress);
        map.put("isdefault",isDefault);
        map.put("sex",sex);
        map.put("cityName",cityName);
        map.put("tag",tag);
        map.put("longitude",lng);
        map.put("latitude",lat);
        map.put("adCode",adCode);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);

        mApiService.addAddress(body)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 5、获取地址列表
     * */
    public void getAddressData(String shopId,BaseHttpObserver<BaseResBean<List<AddressListBean.ResultBean>>> observer) {
        mApiService.getAddressData(shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 6、删除地址
     * */
    public void deleteAddress(int addressId,BaseHttpObserver<BaseResBean> observer) {
        mApiService.deleteAddress(addressId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 7、修改地址
     */
    public void modifyAddress(int addressId,int sex,String userName,String userPhone,String address,String detailAddress,String fullAddress,int isDefault,String cityName,int tag,double lat,double lng,String adCode,BaseHttpObserver<BaseResBean> observer) {
        Map map = new HashMap();
        map.put("id",addressId);
        map.put("sex",sex);
        map.put("contactperson",userName);
        map.put("contactphone",userPhone);
        map.put("address",address);
        map.put("fulladdress",fullAddress);
        map.put("attachaddress",detailAddress);
        map.put("isdefault",isDefault);
        map.put("cityName",cityName);
        map.put("tag",tag);
        map.put("longitude",lng);
        map.put("latitude",lat);
        map.put("adCode",adCode);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);

        mApiService.modifyAddress(body)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 8、判断当前地址是否支持配送,并返回配送价格和距离
     * */
    public void judgeDelivery(int addressId,String shopId,BaseHttpObserver<BaseResBean<AddressBackPriceBean.ResultBean>> observer) {
        mApiService.judgeDelivery(addressId,shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 9、修改默认地址
     * */
    public void modifyDefaultAddress(int addressId,BaseHttpObserver<BaseResBean> observer) {
        mApiService.modifyDefaultAddress(addressId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 10、获取购物车数据
     * */
    public void getShopCarData(String shopId,int addrId,BaseHttpObserver<BaseResBean<List<ShopCarDataBean.ResultBean>>> observer) {
        mApiService.getShopCarData(shopId,addrId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 11、获取计算默认地址
     * */
    public void getSettleDefaultAddress(String shopId,BaseHttpObserver<BaseResBean<SettleDefaultAddressBean.ResultBean>> observer) {
        mApiService.getSettleDefaultAddress(shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 12、用户下单
     */
    public void userDownOrder(int creditToMoney, int deliveryType, int distance, int freight, int addressId, int paymentPrice, String shopId, List<ShopCarDataBean.ResultBean> shopCarDataBean, String remark,String mobile,String name,BaseHttpObserver<BaseResBean<DownOrderBean.ResultBean>> observer) {
        Map map = new HashMap();
        map.put("deliveryType",deliveryType);
        map.put("addressId",addressId);
        map.put("paymentPrice",paymentPrice);
        map.put("shopId",shopId);
        map.put("memo",remark);
        map.put("appInfo",new AppInfo());
        if(!TextUtils.isEmpty(mobile)){
            map.put("mobile",mobile);
        }
        if(!TextUtils.isEmpty(name)){
            map.put("name",name);
        }
        String json = new Gson().toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);

        mApiService.userDownOrder(body)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 13、获取支付参数
     */
    public void getPayParam(String orderId, String wxPay,String payPassword,BaseHttpObserver<BaseResBean<PayInfoResponse.ResultBean>> observer) {

        Map<String, Object>  requestDataMap = new HashMap();
        Map<String, String>  appInfo = new HashMap();
        appInfo.put("bundleId","com.bsit.coband");
        appInfo.put("appVersion", BuildConfig.VERSION_NAME);
        appInfo.put("sourceSystem","1");

        requestDataMap.put("tradeId",orderId);
        requestDataMap.put("payWay",wxPay);
        requestDataMap.put("payPassword",payPassword);
        requestDataMap.put("appId","AP577892496042885120");
        requestDataMap.put("accessToken",ConstantInfo.h5UserToken);
        requestDataMap.put("appInfo",appInfo);
        if(wxPay.equals("CLOUDPAY")){
            requestDataMap.put("payChannel","CLOUDPAY");
            requestDataMap.put("payModel","MWEB");
        }

        mApiService.getPayParam(requestDataMap)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 14、获取订单详情
     * */
    public void getOrderDetailInfo(String orderId,BaseHttpObserver<BaseResBean<OrderDetailBean.ResultBean>> observer) {
        mApiService.getOrderDetailInfo(orderId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 15、获取支付信息接口
     */
    public void getPayResultInfo(String tradeId, BaseHttpObserver<BaseResBean<PayResultBean.ResultBean>> observer) {
        Map map = new HashMap();
        map.put("tradeId",tradeId);
        map.put("appId","AP577892496042885120");
        map.put("accessToken",ConstantInfo.h5UserToken);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);

        mApiService.getPayResultInfo(body)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /** 获取医保余额接口 */
    public void getInsuranceBalance( BaseHttpObserver<BaseResBean<InsuranceUrl>> observer) {
        mApiService.getInsuranceBalance()
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /** 获取医保签约接口 */
    public void getInsuranceRegisterUrl( BaseHttpObserver<BaseResBean<InsuranceUrl>> observer) {
        mApiService.getInsuranceRegisterUrl()
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

}
